package com.example.animation_01;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnimationActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mImage;
    private Button mAlphaButton;
    private Button mRotateButton;
    private Button mScaleButton;
    private Button mTranslateButton;
    private Button mSetButton;
    private TextView mText1;
    private TextView mText2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        mImage = findViewById(R.id.image);
        mAlphaButton = findViewById(R.id.btn_alpha);
        mRotateButton = findViewById(R.id.btn_rotate);
        mScaleButton = findViewById(R.id.btn_scale);
        mTranslateButton = findViewById(R.id.btn_translate);
        mSetButton = findViewById(R.id.btn_set);
        mText1 = findViewById(R.id.text1);
        mText2 = findViewById(R.id.text2);
        mAlphaButton.setOnClickListener(this);
        mRotateButton.setOnClickListener(this);
        mScaleButton.setOnClickListener(this);
        mTranslateButton.setOnClickListener(this);
        mSetButton.setOnClickListener(this);
        mText2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_alpha) {
            //startAlphaAnimXML();
            startAlphaAnimJava();
        } else if (id == R.id.btn_rotate) {
            //startRotateAnimXML();
            startRotateAnimJava();
        } else if (id == R.id.btn_scale) {
            //startScaleAnimXML();
            startScaleAnimJava();
        } else if (id == R.id.btn_translate) {
            startTranslateAnimXML();
            //startTranslateAnimJava();
        } else if (id == R.id.btn_set) {
            //setSetAnimXML();
            startSetAnimJava();
        } else if (id == R.id.text2) {
            startText2Anim();
        }
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //overridePendingTransition(R.anim.no_anim, R.anim.alpha_out);
    }
    private void startText2Anim() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startText1Anim();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mText2.startAnimation(alphaAnimation);
    }

    private void startText1Anim() {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 1);
        scaleAnimation.setDuration(1000);
        scaleAnimation.setFillAfter(true);
        mText1.startAnimation(scaleAnimation);
    }

    private void setSetAnimXML() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.animations);
        mImage.startAnimation(animation);
    }

    private void startSetAnimJava() {
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.RELATIVE_TO_PARENT, 0.5F);
        RotateAnimation rotateAnimation = new RotateAnimation(
                0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5F);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.setDuration(1500);
        mImage.startAnimation(animationSet);
    }

    private void startTranslateAnimXML() {
        Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
        mImage.startAnimation(translateAnimation);
    }

    private void startTranslateAnimJava() {
        //TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 0);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.RELATIVE_TO_PARENT, 0.5F);

        translateAnimation.setDuration(1000);
        translateAnimation.setInterpolator(new LinearInterpolator());

        mImage.startAnimation(translateAnimation);
    }

    private void startScaleAnimXML() {
        Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
        mImage.startAnimation(scaleAnimation);
    }

    private void startScaleAnimJava() {
        //ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2);
//        ScaleAnimation scaleAnimation = new ScaleAnimation(
//                1, 2, 1, 2, 300, 300);
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                1, 2, 1, 2,
                Animation.RELATIVE_TO_SELF, 0.5F,
                Animation.RELATIVE_TO_SELF, 0F);

        scaleAnimation.setDuration(1000);
        //scaleAnimation.setRepeatCount(4);
        //scaleAnimation.setRepeatMode(Animation.REVERSE);
        //scaleAnimation.setFillAfter(true);
        scaleAnimation.setInterpolator(new BounceInterpolator());
        mImage.startAnimation(scaleAnimation);
    }

    private void startRotateAnimXML() {
        Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        mImage.startAnimation(rotateAnimation);
    }

    private void startRotateAnimJava() {
        //RotateAnimation rotateAnimation = new RotateAnimation(0, 180);
        //RotateAnimation rotateAnimation = new RotateAnimation(0, 180, 100, 100);
        RotateAnimation rotateAnimation = new RotateAnimation(
                0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5F);
        rotateAnimation.setDuration(1000);
        //rotateAnimation.setRepeatCount(3);
        //rotateAnimation.setRepeatMode(Animation.REVERSE);
        rotateAnimation.setInterpolator(new BounceInterpolator());
        mImage.startAnimation(rotateAnimation);
    }

    private void startAlphaAnimXML() {
        Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
        mImage.startAnimation(alphaAnimation);
    }

    private void startAlphaAnimJava() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 0.2F);
        alphaAnimation.setDuration(1000);
        //alphaAnimation.setFillAfter(true);
        //alphaAnimation.setRepeatCount(5);
        //alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setInterpolator(new BounceInterpolator());
        mImage.startAnimation(alphaAnimation);
    }
}